package com.ccl.iot.template;import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout.LayoutParams;

import com.ccl.iot.R;
import com.ccl.iot.privates.BitmapBuilder;

public class ActivityWithMenu extends ActivityTitled {
	public static final int KMENU_PADDING = 10;
	public static final int KMENU_BACK_COLOR = Color.DKGRAY;

	/*
	 * 	Non-static functions
	 */
	private ImageButtonView iMenuButton = null;
	private LinearLayout iMenuContent = null;
	private PopupWindow iMenuWindow = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		iMenuButton = new ImageButtonView(this.getContext());
		iMenuButton.setImageBitmap(BitmapBuilder.GetBitmap(this.getContext(), R.drawable.icon_menu, false));
		iMenuButton.setVisibility(View.GONE);
		super.addRightTitleView(iMenuButton);
		
		handleMenuButton();
	}
	
	public void setMenuBitmap(Bitmap aBitmap){
		if (iMenuButton != null){
			iMenuButton.setImageBitmap(aBitmap);
		}
	}
	
	public boolean addMenu(View aMenu){
		if (aMenu != null){
			if (iMenuButton != null){
				if (iMenuContent == null){
					iMenuContent = new LinearLayout(this);
					iMenuContent.setOrientation(LinearLayout.VERTICAL);
					iMenuContent.setBackgroundColor(KMENU_BACK_COLOR);
					iMenuContent.setPadding(KMENU_PADDING, KMENU_PADDING, KMENU_PADDING, KMENU_PADDING);
				}
				
				if (iMenuContent != null){
					LinearLayout tFrame = new LinearLayout(this);
					
					tFrame.setOrientation(LinearLayout.VERTICAL);
					
					if (iMenuContent.getChildCount() > 0){
						tFrame.setBackgroundColor(Color.BLACK);
						tFrame.setPadding(0, 1, 0, 0);
						
						aMenu.setBackgroundColor(KMENU_BACK_COLOR);
					}
					
					tFrame.addView(aMenu, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
					
					iMenuContent.addView(tFrame, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
					
					if (iMenuWindow == null){
						iMenuWindow = new PopupWindow(this);
						
						iMenuWindow.setOutsideTouchable(true);
						
						iMenuWindow.setTouchInterceptor(new View.OnTouchListener() {
							@Override
							public boolean onTouch(View v, MotionEvent event) {
								return (event.getY() < 0 && event.getX() >= iMenuContent.getWidth() - iMenuButton.getWidth());
							}
						});
						
						//==============modify start==========================

						//==============modify end==========================
					}
				}
				
				if (iMenuButton.getVisibility() == View.GONE){
					iMenuButton.setVisibility(View.VISIBLE);
				}
			}
		}
		
		return false;
	}

	@Override
	public void addRightTitleView(View aView) {
		int tCount = super.getRightTitleViewCount();
		
		if (tCount == 1){
			super.addRightTitleView(aView, 0);
		}else{
			super.addRightTitleView(aView, tCount - 1);
		}
	}
	
	@Override
	public void removeRightTitleView(int aIndex){
		removeRightTitleView(aIndex);
	}
	
	@Override
	public void removeAllRightTitleView(){
		for (int i = 0, tCount = super.getRightTitleViewCount() - 1; i < tCount; i++){
			super.removeRightTitleView(0);
		}
	}
	
	private void showMenu(){
		if (iMenuWindow != null){	
			if (iMenuWindow.isShowing() == false){
				View tContentView = super.getContentView();
				int tLocation[] = new int[2];
				
				iMenuWindow.setBackgroundDrawable(new ColorDrawable(0));
				iMenuWindow.setContentView(iMenuContent);
				iMenuWindow.setWidth(LayoutParams.WRAP_CONTENT);
				iMenuWindow.setHeight(LayoutParams.WRAP_CONTENT);
				
				if (tContentView != null){
					tContentView.getLocationOnScreen(tLocation);
				}
				iMenuWindow.showAtLocation(tContentView, Gravity.RIGHT | Gravity.TOP, 0, tLocation[1] + 1);
			}else{
				iMenuWindow.dismiss();
			}
		}
	}

	private void handleMenuButton(){
		if (iMenuButton != null){
			iMenuButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					showMenu();
				}
			});
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU){
			if (iMenuWindow != null){
				showMenu();
				
				return true;
			}
		}else if (keyCode == KeyEvent.KEYCODE_BACK){
			if (iMenuWindow != null && iMenuWindow.isShowing()){
				iMenuWindow.dismiss();
				
				return true;
			}
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
